package com.coderdream.chapter13.visitor.sample;

import java.util.Iterator;

public class ListVisitor extends Visitor {
	
	private String currentdir = "";	// 目錄所在的目錄名稱

	
	
	/**
	 * 訪問文件時調用
	 */
	@Override
	public void visit(File file) {
		System.out.println(this.currentdir + "/" + file);		
	}

	/**
	 * 訪問目錄時調用
	 */
	@Override
	public void visit(Directory directory) {
		System.out.println(this.currentdir + "/" + directory);
		String savedir = this.currentdir;
		this.currentdir = this.currentdir + "/" + directory.getName();
		Iterator it = directory.iterator();
		
		while (it.hasNext()) {
			Entry entry = (Entry) it.next();
			entry.accept(this);
		}
		
		this.currentdir = savedir;		
	}

}
